﻿package com.riadeveloper.UI
{
	import com.greensock.TweenMax;
	import com.greensock.easing.Cubic;
	import com.greensock.easing.Expo;
	import com.riadeveloper.store.GetData;
	import com.riadeveloper.store.TileObj;
	
	import flash.display.Bitmap;
	import flash.display.GradientType;
	import flash.display.InterpolationMethod;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.PixelSnapping;
	import flash.display.Shape;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.AntiAliasType;
	import flash.text.Font;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	public class Tile extends Sprite
	{
		public var referMc:Sprite= new Sprite();
		private var propery:Object= {x:0,y:0,width:0,height:0};
		public var id_num:Number = 0 ;
		
		public var bgMc:Shape = new Shape();
		private var shapeBg:Sprite = new Sprite();
		private var loadShape:Shape = new Shape();
		private var holderMc:Holder = new Holder();
		private var imgContainer:ImageContainer = new ImageContainer ();
		public var tileObj:TileObj = null;
		
		private var this_x:Number = 0;
		private var this_y:Number = 0;
		
		private var _width:Number;
		private var _height:Number;
		private var mc_x:Number = 0;
		private var mc_y:Number = 0;
		private var _x:Number = 0;
		private var _y:Number = 0;
		private var tweenSpeed:Number = .75;
		public var imageOpen:Boolean= false;
		public var imgSrc:String="";
		public var xml:XML = new XML;
		
		public var randomNum:Number= 0;

		private var mainMc:Sprite = new Sprite();
		private var grid:Rectangle;
		private var horzontalPos:String;
		private var verticalPos:String;

		
		var xNum = 0; 
		var yNum = 0;

		
		
		public function Tile(_name:String,mc:Sprite)
		{
			referMc = mc;
			name = _name;
			tileObj =  GetData.GetValue(name);		
			this.x = referMc.x;
			this.y = referMc.y;		
			
			bgMc.graphics.beginFill(0xFFFFFF);
			bgMc.graphics.drawRoundRect(0,0,referMc.width,referMc.height,10);
			bgMc.graphics.endFill();
			addChild(bgMc);
				
			
			
	
		
			
			
					
		}
		public function initialise():void
		{
			randomNum = Math.round(Math.random()*3);
			loadShape.graphics.beginFill(0x999999,.5);
			loadShape.graphics.drawRect(0,0,referMc.width,referMc.height);
			loadShape.graphics.endFill();
			
			
			
			var loader:Loader = new Loader();
			loader.load(new URLRequest(xml.content.image.tileImage));
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadImage);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,loading);

			
			shapeBg = new Sprite();				
			var gradientBoxMatrix:Matrix = new Matrix(); 
			gradientBoxMatrix.createGradientBox(referMc.width,referMc.height, (Math.PI/2), 0, 0);
			shapeBg.graphics.beginGradientFill(GradientType.LINEAR, [0x999999, 0xcccccc,], [1, 1], [0, 255], gradientBoxMatrix); 	
			shapeBg.graphics.drawRoundRectComplex(0,0,referMc.width+tileObj.changeImgArr[id_num]["width"],referMc.height+tileObj.changeImgArr[id_num]["height"],10,10,10,10);
			shapeBg.graphics.endFill();

			shapeBg.addEventListener(MouseEvent.CLICK,openURL);
			shapeBg.buttonMode = true;
			
			mainMc.addChild(shapeBg);
			mainMc.addChild(loadShape);
			
			mainMc.mouseChildren = false;
			
			addChild(mainMc);			
			mainMc.mask = bgMc
		
		}
		private function loading(e:ProgressEvent):void
		{
			var per:Number = e.bytesLoaded/e.bytesTotal;
			
			switch(randomNum)
			{
				case 0:
					loadShape.scaleX = 1;
					loadShape.scaleY = per;
					break;
				case 1:
					loadShape.scaleX = 1;
					loadShape.scaleY = per;
					break;
				case 2:
					loadShape.scaleX = per;
					loadShape.scaleY = 1;
					break;
				case 3:
					loadShape.scaleX = per;
					loadShape.scaleY = 1;
					break;
			}
			
		}
		public function loadImage(e:Event):void
		{	
			TileHolder(this.parent).LoadedTile();
			mainMc.removeChild(loadShape);
			Bitmap(e.currentTarget.content).smoothing = true;
			imgContainer.bitmap = Bitmap(e.currentTarget.content);
			Bitmap(e.currentTarget.content).smoothing = true;
			Bitmap(e.currentTarget.content).pixelSnapping =PixelSnapping.NEVER;
			imgContainer.bitmap.pixelSnapping =PixelSnapping.NEVER;
			imgContainer.bitmap.smoothing = true;
			imgContainer.img.addChild(imgContainer.bitmap);
			imgContainer.img.alpha = 0;
			TweenMax.to(imgContainer.img,.5,{alpha:1})
			imgContainer.createMask(referMc.width+40,referMc.height+40);
		
			imgContainer.img.x = xml.content.image.position.x;
			imgContainer.img.y = xml.content.image.position.y;
			
			if(tileObj.style =="IMAGE")
			{
				var _linkMc:linkMc = new linkMc();
				_linkMc.linkTxt.autoSize = TextFieldAutoSize.LEFT;
				_linkMc.linkTxt.text = xml.content.image.activeState.callToAction.text;
				_linkMc.btn.x = _linkMc.linkTxt.width+20;
				var _linkBg:linkBg = new linkBg();			
				
				var cssStyle:StyleSheet = new StyleSheet();
				var header:Object = new Object();
				header.fontSize = 22;
				header.fontFamily = "calibri";
				cssStyle.setStyle(".header", header); 
				var header1:Object = new Object();
				header1.fontSize = 16;
				header1.fontFamily = "calibri";
				cssStyle.setStyle(".header1", header1); 
				var content:Object = new Object();
				content.fontSize = 12;
				content.fontFamily = "calibri";
				cssStyle.setStyle(".content", content); 
				
				var textBox:TextField = new TextField();
				textBox.wordWrap = true;
				textBox.multiline = true;
				textBox.styleSheet = cssStyle;
				textBox.width=120;
				textBox.selectable = false;
				
				if(tileObj.changeImgArr[id_num]["height"] > 41)
					textBox.width=referMc.width+20;
				
				textBox.htmlText = xml.content.image.activeState.descriptionText;
				
				
				if(tileObj.changeImgArr[id_num]["width"] < 45 || tileObj.changeImgArr[id_num]["x"] < 5 )
				{
					_linkMc.x = referMc.width+tileObj.changeImgArr[id_num]["width"]-_linkMc.width-20;
				}
				else 
				{
					_linkMc.x = tileObj.changeImgArr[id_num]["width"]-_linkMc.width-60;
				}				
				
				if(tileObj.textBoxPosV =="top" && tileObj.changeImgArr[id_num]["height"] > 41 )
				{					
					_linkMc.y = tileObj.changeImgArr[id_num]["height"]-60;
					
				}else {					
					_linkMc.y = referMc.height+tileObj.changeImgArr[id_num]["height"]-30;					
				}
				
				if(tileObj.textBoxPos =="right"&& tileObj.changeImgArr[id_num]["x"] < 0  )
				{
					textBox.x = referMc.width+50;
				}
				else 
				{
					textBox.x = 10;
				}
				if(tileObj.textBoxPosV =="bottom" && tileObj.changeImgArr[id_num]["y"] <= -41  )
				{
					textBox.y = referMc.height+50;
				}
				else 
				{
					textBox.y = 10;
				}
				
				_linkBg.x  = _linkMc.x;
				_linkBg.y  = _linkMc.y;
				
				mainMc.addChild(textBox);
				mainMc.addChild(_linkBg);
				mainMc.addChild(_linkMc);
				
				TweenMax.allTo([textBox,_linkBg,_linkMc],0,{alpha:0});
				TweenMax.allTo([textBox,_linkBg,_linkMc],.5,{alpha:1,delay:.2});
			}
			mainMc.addChild(imgContainer);
			
		
		}
		private function openURL(e:MouseEvent):void
		{
			trace("sdf")
			if(xml.content.image.activeState.callToAction.linkType == "INTERNAL")
			{
				navigateToURL(new URLRequest( xml.content.image.activeState.callToAction.linkUrl),"_self")
			}else 
			{
				navigateToURL(new URLRequest( xml.content.image.activeState.callToAction.linkUrl),"_blank")
			}
		}
		public function setEvent():void
		{
			mainMc.addEventListener(MouseEvent.CLICK,openURL);
			mainMc.mouseChildren = false ;
			mainMc.buttonMode = true;
			horzontalPos = String(xml.content.image.viewpoint).substr(0,1);
			verticalPos = String(xml.content.image.viewpoint).substr(1,1);
			addEventListener(MouseEvent.ROLL_OVER,TileHolder(this.parent).open);
			addEventListener(MouseEvent.ROLL_OUT,TileHolder(this.parent).close);
		}
		

			
		
		

		public function changePosition(obj:Object,_mousePos,allow:Boolean =false):void
		{
				if(obj ==null)
					obj = new Object();
				for(var id:String in propery) {
					if(obj[id] != null)
					{
						var value:Number = obj[id]*_mousePos;
						if(id =="x" ||  id =="y" )
						{					
							this[id] += (referMc[id]+value - this[id])/8;	
							
						}else {
							bgMc[id] += (referMc[id]+value-bgMc[id])/8;
							
							
						}
					}else {
						if(id =="x" ||  id =="y" )
						{					
							this[id] += (referMc[id]- this[id])/8;
						}else {
							bgMc[id]	+= (referMc[id]-bgMc[id])/8 ;
						}
						
					}			
				}
				if(obj["imgX"] != undefined)
					imgContainer.img.x += ((obj["imgX"]*_mousePos)- imgContainer.img.x)/8
				
				/*if(allow)
				{
					trace(xml.content.image.position.x)
					switch(horzontalPos)
					{
						case "L":
							break;
						case "R":
							imgContainer.img.x += ((xml.content.image.position.x+bgMc.width-referMc.width)-imgContainer.img.x)/6;
							break;
						case "C":
							imgContainer.img.x += (xml.content.image.position.x+((bgMc.width-referMc.width)/2)-imgContainer.img.x)/6;
							break;
					}
					switch(verticalPos)
					{
						case "T":
							break;
						case "B":
							imgContainer.img.y += ((xml.content.image.position.y+bgMc.width-referMc.height)-imgContainer.img.x)/6;
							break;
						case "C":
							imgContainer.img.y += (xml.content.image.position.y+((bgMc.height-referMc.height)/2)-imgContainer.img.y)/6;
							break;
					}
					
				}*/
				
		}
		public function resetPosition():void
		{
			for(var id:String in propery) {
				if(id =="x" ||  id =="y" )
				{					
					this[id] += (referMc[id]- this[id])/8;						
				}else {
					bgMc[id]	+= (referMc[id]-bgMc[id])/8 ;						
				}
			}		
		}
		public function imageMove(value:Number):void
		{
			if(tileObj.textBoxPosH =="left" && tileObj.changeImgArr[id_num]["width"] > 41 && tileObj.changeImgArr[id_num]["x"] < 0 ){
				//imgContainer.x += ((tileObj.changeImgArr[id_num]["width"]-40)*value-imgContainer.x )/8;
			}else if(tileObj.textBoxPosH =="left" && tileObj.changeImgArr[id_num]["width"] > 41 ){
				imgContainer.x += ((tileObj.changeImgArr[id_num]["width"]-40)*value-imgContainer.x )/8;
			}
			
			if(tileObj.textBoxPosV =="top" && tileObj.changeImgArr[id_num]["height"] > 41 ){
				imgContainer.y += ((tileObj.changeImgArr[id_num]["height"]-40)*value-imgContainer.y )/8;
			}
			imgContainer.img.x += (xml.content.image.zoomPosition.x - imgContainer.img.x)/8;
			imgContainer.img.y += (xml.content.image.zoomPosition.y - imgContainer.img.y)/8;
			imgContainer.img.scaleX = imgContainer.img.scaleY += (.95-imgContainer.img.scaleY)/8;
		
		}
		public function resetImageMove():void
		{
			imgContainer.x += (-imgContainer.x )/8;
			imgContainer.y += (-imgContainer.y )/8;
			imgContainer.img.x += (xml.content.image.position.x - imgContainer.img.x)/8;
			imgContainer.img.y += (xml.content.image.position.y - imgContainer.img.y)/8;
			imgContainer.img.scaleX = imgContainer.img.scaleY += (1-imgContainer.img.scaleY)/8
	
		}
	}
}